package com.quectel.cms.controller.property;

import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.property.dto.PropertyInfoDto;
import com.quectel.core.module.property.service.PropertyInfoService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-23 09:09:53
 */
@RestController
@RequestMapping("propertyInfo")
@Tag(name = "PropertyInfoController", description = "物业基本信息表相关api")
public class PropertyInfoController extends BaseController {

    @DubboReference
    private PropertyInfoService propertyInfoService;

    /**
     * 物业基本信息分页查询列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "propertyInfo:list")
    @Operation(summary = "物业基本信息分页查询列表")
    public Response<Page<PropertyInfoDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = propertyInfoService.queryTotal(request.getParams());

        List<PropertyInfoDto> list = null;
        if (total > 0) {
            list = propertyInfoService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PropertyInfoDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PropertyInfoDto>>ok().wrap(page);
    }


    /**
     * 查询物业基本信息单条记录
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "propertyInfo:info")
    @Operation(summary = "查询物业基本信息单条记录")
    public Response<PropertyInfoDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (propertyInfoService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        PropertyInfoDto propertyInfoDto = propertyInfoService.selectById(id);

        return Response.<PropertyInfoDto>ok().wrap(propertyInfoDto);
    }

    /**
     * 保存物业基本信息单条记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "propertyInfo:edit")
    @Operation(summary = "保存物业基本信息单条记录")
    public Response<Object> save(@RequestBody @Validated PropertyInfoDto propertyInfoDto) {

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("villageId", propertyInfoDto.getVillageId());
        int total = propertyInfoService.queryTotal(paramMap);
        if (total > 0) {
            return Response.error("保存失败,小区信息已存在");
        } else {

            //填充基础数据
            fillSaveCommonData(propertyInfoDto);

            propertyInfoService.save(propertyInfoDto);

            return Response.ok();
        }
    }

    /**
     * 修改物业基本信息单条记录
     */
    @PostMapping("update")
    @RequirePermissions(values = "propertyInfo:edit")
    @Operation(summary = "修改物业基本信息单条记录")
    public Response<Object> update(@RequestBody @Validated PropertyInfoDto propertyInfoDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(propertyInfoDto.getId()));
        if (propertyInfoService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(propertyInfoDto);

        propertyInfoService.updateById(propertyInfoDto);

        return Response.ok();
    }

    /**
     * 批量删除物业基本信息记录
     */
    @PostMapping("delete")
    @RequirePermissions(values = "propertyInfo:delete")
    @Operation(summary = "批量删除物业基本信息记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (propertyInfoService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        propertyInfoService.deleteBatch(ids);

        return Response.ok();
    }
}
